/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.biyao.codegenerator.config;

import com.biyao.codegenerator.dao.*;
import com.biyao.codegenerator.utils.ReException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * 数据库配置
 *
 * @author Mark sunlightcs@gmail.com
 * @since 2018-07-24
 */
@Configuration
public class DbConfig {
    @Value("${renren.database: mysql}")
    private String database;
    @Autowired
    private MySqlGeneratorDao mySQLGeneratorDao;
    @Autowired
    private OracleGeneratorDao oracleGeneratorDao;
    @Autowired
    private SqlServerGeneratorDao sqlServerGeneratorDao;
    @Autowired
    private PostgreSqlGeneratorDao postgreSQLGeneratorDao;

    @Bean
    @Primary
    public GeneratorDao getGeneratorDao() {
        final String mysql = "mysql";
        final String oracle = "oracle";
        final String postgresql = "postgresql";
        final String sqlserver = "sqlserver";
        if (mysql.equalsIgnoreCase(database)) {
            return mySQLGeneratorDao;
        } else if (oracle.equalsIgnoreCase(database)) {
            return oracleGeneratorDao;
        } else if (sqlserver.equalsIgnoreCase(database)) {
            return sqlServerGeneratorDao;
        } else if (postgresql.equalsIgnoreCase(database)) {
            return postgreSQLGeneratorDao;
        } else {
            throw new ReException("不支持当前数据库：" + database);
        }
    }
}
